<?php
include("lib.php");
switch (a(A00)){
    case A01:// 机器列表
        $a = F01(r(R01),true);//发送请求
        $list = [];
        $count = 0;
        global $pre;
        foreach ($a as $v){
            $p = str_replace("\\","/",$v[P04]);
            $p = explode("/", $p);//
            $p = $p[count($p) - 1];
            $p = str_replace(".vmx", "", $p);
            if ($p!==$pre."DC") {
                if (substr($p, 0, strlen($pre)) == $pre) {
                    $list[$v[P01]] = $p;
                    //$list[]=[$v[P01]=>$p];
                    $count++;
                }
            }
            @touch(S03);
            F04(S03, $count);
        }
        b(I01,true,$list);
        break;
    case A02:// 获取电源状态
        $id = a(P01);
        if (empty($id)) b(E02);
        b(I01,true,F01(r(R02,$id),true));
        break;
    case A03:// 改变电源状态
        $id = a(P01);
        if (empty($id))b(E02);
        $s = a(P02);
        if (empty($s))b(E03);
        if ($s==="reboot"){
            r(R03, $id, "off");
            b(I01, false, F01(r(R03, $id, "on"), true));
        }else {
            $a = F01(r(R03, $id, $s), true);
            if (strpos($a["Message"], "Not enough physical memory")>0){
                b(E12);
            }else {
                b(I01, true, $a);
            }
        }
        break;
    case A04:// 获取信息
        $id = a(P01);
        $vmPath = c("vmPath");
        if (empty($id))b(E02);
        $a = F01(r(R01),true);
        $info=[];
        foreach ($a as $v){
            if($v["id"]===$id){
                $p = $v["path"];
                $p=explode("/",str_replace("\\","/",$p));
                $p=$p[count($p)-1];
                $p = str_replace(".vmx","",$p);
                $name=$p;
                $p="$vmPath/$p/$p.vmdk";
	            if(!file_exists($p)){b(E11);}
                $fp = fopen($p,"r");
                $str=fread($fp, 1024);
                $str=str_replace("\r","\n",$str);
                $str=explode("\n",$str);
                foreach ($str as $sv){
                    if (substr($sv,0,2)==="RW"){
                        $sv = (int)str_replace(["RW","SPARSE","\"$name.vmdk\""," "],[""],$sv);
                        $sv/=2;
                        $ext = "";
                        if ($sv>=1024){
                            $sv/=1024;
                            $ext="M";
                        }
                        if ($sv>=1024){
                            $sv/=1024;
                            $ext="G";
                        }
                        if ($sv>=1024){
                            $sv/=1024;
                            $ext="T";
                        }
                        $info["disk"]=$sv.$ext."B";
                        goto L1;
                    }
                }
                b(I01,true,$p);
            }
        }
        b(E10);
        L1:
        $a = F01(r(R04,$id),true);
        $info["cpu"]=$a["cpu"]["processors"]."核";
        $mem = $a["memory"];
        $ext = "M";
        if ($mem>=1024){
            $mem/=1024;
            $ext = "G";
        }
        $info["memory"]=$mem.$ext."B";
        b(I01,false,F02($info));
        if(a("debug")==="yes"){
            var_dump($info);
        }
        break;
    case A05:// 重装系统
        $id = a(P01);
        if (empty($id))b(E02);
        $a = r(R03,$id,"off");
        break;
    case A06:// 机器状态
        b(I01);
        break;
    case A07:// 机器数量
        $count = 0;
        if(file_exists(S03)){
            $count = (int)F03(S03);
        }
        b(I01,true,$count);
        break;
    case A08:// 设置接口秘钥
        $a = a(P05);
        $b = a(P06);
        if (empty($a))
            b(E04);
        elseif (empty($b))
            b(E05);
        else{
            $c = F03(S02);
            if ($c){
                $c=F01($c,true);
                $c["token"] = base64_encode("$a:$b");
                $c=F02($c);
                $d = F04(S02,$c);
                if ($d)
                    b(I01);
                else
                    b(E07);
            }else
                b(E06);
        }
        break;
    case A09:// 初始化配置文件
        @touch(S02);
        @F04(S02,F02(S99));
        b(I01);
        break;
    case A10:// 设置路径
        $a = a(P03);
        $b = F01(F03(S02),true);
        $c = a(P07);
        if ($a === "vm"){
            $b["vmPath"] = $c;
        }elseif ($a === "disk"){
            $b["diskPath"] = $c;
        }else{
            b(E01);
        }
        $d = F04(S02,F02($b));
        if ($d){
            b(I01);
        }else{
            b(E07);
        }
        break;
    case A11:// 设置端口前缀
        $a = a(P07);
        $b = F01(F03(S02),true);
        $b["portPrefix"]=$a;
        $c = F04(S02,F02($b));
        if ($c){
            b(I01);
        }else{
            b(E07);
        }
        break;
    case A12:// 获取端口前缀
        b(I01,false,c("portPrefix"));
        break;
    case A13:// 获取操作系统列表
        $data = ["Windows"=>[],"Linux"=>[]];
        $a = scandir(c("diskPath"));
        foreach ($a as $item) {
            if ($item!=="."and$item!==".."){
                if (substr($item,strlen($item)-5)===".vmdk"){
                    $item = substr($item,0,strlen($item)-5);
                    if (substr($item,0,7)==="Windows"){
                        $data["Windows"][]=$item;
                    }else{
                        $data["Linux"][]=$item;
                    }
                }
            }
        }
        b(I01,false,$data);
        break;
    case A14:
        $data = [];
        $data["vm"] = c("vmPath");
        $data["vdisk"] = c("diskPath");
        b(I01,true,$data);
        break;
    case A99:// 调试
//        print_r(scandir(""));
//        var_dump(r(R01));
        $d = a("controlName");
        $b = a("id");
        $c = a("data");
        if (@$_GET["clear"]==="yes"){
            file_put_contents("mission", "{\"missions\":[]}");
        }else {
            $a = json_decode(@file_get_contents("mission"),true);
            $a["missions"][]=["control"=>$d,"id"=>$b,"data"=>$c];
            file_put_contents("mission",json_encode($a));
        }
        break;
    default:
        b(E01);
        break;
}


